{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8f45d7a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f24d04f6",
   "metadata": {},
   "source": [
    "### 广播机制\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3d77f87",
   "metadata": {},
   "source": [
    "#### 一维数组广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "53761995",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[0,1,2,3] * 4\n",
    "# *4就是变成，4组 0，1，2，3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0784e5ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# sort 排序\n",
    "# [0,1,2,3]*3\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) #shape(4,3)\n",
    "display(arr1)\n",
    "arr2 = np.array([1,2,3]) # shape(3,)\n",
    "# arr2 = np.array([1.2.3.4])\n",
    "# 这样是不行的，因为行和列是不对称的\n",
    "# 行只有三个，而 1，2，3，4的话出现了四个，不对称\n",
    "display(arr2)\n",
    "arr3 = arr1 + arr2 # arr2进行广播复制4份 shape(4,3)\n",
    "arr3 \n",
    "# 主jupyter会将变量输出\n",
    "# 一维是行方向的广播"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7eb87837",
   "metadata": {},
   "source": [
    "#### 二维数组广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7a9334ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.sort(np.array([0,1,2,3]*3)).reshape(4,3) # shape(4,3)\n",
    "# arr2，二维\n",
    "arr2 = np.array([[1],[2],[3],[4]]) # shape(4,1)\n",
    "# 同样道理 【【1】，【2】，【3】，【4】。【5】】 这样也是不行的\n",
    "display(arr1,arr2)\n",
    "arr3 = arr1 + arr2 # arr2 进行广播复制3份 shape(4,3)\n",
    "display(arr1,arr2,arr3)\n",
    "# 二维是列方向的一个广播"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66a7b5cd",
   "metadata": {},
   "source": [
    "#### 高维数组广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5abe77ca",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]],\n",
       "\n",
       "       [[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 4, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[6, 0]],\n",
       "\n",
       "       [[1, 6]],\n",
       "\n",
       "       [[0, 7]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 1, 2)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[[ 3,  4],\n",
       "        [ 5,  6],\n",
       "        [ 7,  8],\n",
       "        [ 9, 10]],\n",
       "\n",
       "       [[ 1, 10],\n",
       "        [ 3, 12],\n",
       "        [ 5, 14],\n",
       "        [ 7, 16]],\n",
       "\n",
       "       [[ 5,  4],\n",
       "        [ 7,  6],\n",
       "        [ 9,  8],\n",
       "        [11, 10]]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([0,1,2,3,4,5,6,7]*3).reshape(3,4,2) #shape(3,4,2)\n",
    "# arr2 = np.array([0,1,2,3,4,5,6,7]).reshape(4,2) #shape(4,2)\n",
    "# arr2 = np.random.randint(0,10,size = (3,4,1))\n",
    "arr2 = np.random.randint(0,10,size = (3,1,2))\n",
    "display(arr1,arr1.shape,arr2,arr2.shape)\n",
    "arrr3 = arr1 + arr2 # arr2数组在0维上复制3份 shape(3,4,2)\n",
    "arr3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5960fdb5",
   "metadata": {},
   "source": [
    "### 通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13c920a3",
   "metadata": {},
   "source": [
    "#### 元素级别数字函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1059ac3",
   "metadata": {},
   "source": [
    "abs、sqrt、square、exp、log、sin、cos、tan，maxinmum、minimum、all、any、inner、clip、round、trace、ceil、floor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dccffd30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.         2.         2.82842712 3.         4.         5.        ]\n",
      "[  1  16  64  81 256 625]\n",
      "[ 2  4  8  9 16 16]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,4,8,9,16,25])\n",
    "print(np.sqrt(arr1)) # 开平方\n",
    "print(np.square(arr1)) # 平方\n",
    "print(np.clip(arr1,2,16))\n",
    "# 最大不能超过16，最小不能小于2\n",
    "# array([ 2,  4,  8,  9, 16, 16])\n",
    "# clip 裁剪的意思，给一个范围给裁剪掉。\n",
    "# 原来的1变成了2，原来的25变成了16.\n",
    "# 参考下面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "6bdec1de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.71828183,  7.3890561 , 20.08553692])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(np.array([1,2,3]))\n",
    "# 其实就是幂运算\n",
    "# 以自然底数为底，进行的幂运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6f36c0fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.718281828459045"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.e\n",
    "# 自然底数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ac2fead",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.085536923187664"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.e**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1798349c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 5 3 9 3 9 8]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1,5,2,9,3,6,8])\n",
    "y = np.array([2,4,3,7,1,9,0])\n",
    "print(np.maximum(x,y)) \n",
    "# 返回两个数组中的比较大的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9adba980",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([173, 132, 141, 156,  83])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,10,size = (5,5))\n",
    "np.inner(arr2[0],arr2)\n",
    "# 返回一维数组向量内积\n",
    "# 向量内积，1行乘1列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "435fd53f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 0, 3, 6, 8],\n",
       "       [0, 3, 2, 9, 9],\n",
       "       [0, 9, 9, 7, 9],\n",
       "       [6, 7, 0, 6, 9],\n",
       "       [1, 3, 3, 3, 6]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([8, 0, 3, 6, 8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(arr2,arr2[0])\n",
    "# 向量内积，1行乘1列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "08ada77d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "173"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(arr2[0] * arr2[0]).sum()\n",
    "# 也就是说 [8,0,3,6,8]变成列，与每一行相乘得出上面的结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d256031c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "173\n",
      "132\n",
      "141\n",
      "156\n",
      "83\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print((arr2[0] * arr2[i]).sum())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06fef4eb",
   "metadata": {},
   "source": [
    "#### where函数使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d85793a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  5,  7, 10])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,3,5,7,9])\n",
    "arr2 = np.array([2,4,6,8,10])\n",
    "cond = np.array([True,False,True,True,False])\n",
    "np.where(cond,arr1,arr2)# True 选择arr1，False选择arr2\n",
    "# True 选x ， Fasle 选y\n",
    "# x和y的长度必须一致，如果不一致就会报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e1c38c67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([23, 15,  2, 18,  7, 23, 25, 20,  8,  5, 11, 24, 12, 25, 14, 10, 12,\n",
       "       18, 21, 10])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([-15, -15,   2, -15,   7, -15, -15, -15,   8,   5,  11, -15,  12,\n",
       "       -15,  14,  10,  12, -15, -15,  10])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.random.randint(0,30,size = 20)\n",
    "display(arr3)\n",
    "np.where(arr3 < 15,arr3,-15)\n",
    "# 小于15还是自身的值，大于15设置成 -\n",
    "# 根据以下的代码，满足条件的话返回原来的值\n",
    "# 如果不满足条件则返回 -15."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b982905",
   "metadata": {},
   "source": [
    "#### 排序方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1aad2f4f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "display(arr)\n",
    "arr.sort() # 直接改变原数组,按照从小到大的顺序排列\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "29dc62a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "arr2 = np.sort(arr) # 返回深拷贝的结果\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e085b535",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr # 原数据不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f6dd75d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([8, 1, 6, 5, 3, 0, 2, 7, 4])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('int64')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([9,3,11,6,17,5,4,15,1])\n",
    "index = arr.argsort() #返回从小到大排序索引\n",
    "display(arr,index,index.dtype)\n",
    "# 根据索引来排序\n",
    "arr[index]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d96d520f",
   "metadata": {},
   "source": [
    "#### 集合运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "8a755588",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4 6]\n",
      "[2 3 4 5 6 8]\n",
      "[2 8]\n"
     ]
    }
   ],
   "source": [
    "A = np.array([2,4,6,8])\n",
    "B = np.array([3,4,5,6])\n",
    "print(np.intersect1d(A,B)) # 交集 array([4, 6])\n",
    "print(np.union1d(A,B)) # 并集 array([2, 3, 4, 5, 6, 8])\n",
    "print(np.setdiff1d(A,B)) # 差集 ，A中有，B中没有"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82177d70",
   "metadata": {},
   "source": [
    "#### 数学和统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82de4a4b",
   "metadata": {},
   "source": [
    "min、max、mean、median、sum、std、var、cumsum、cumprod、argmin、argmax、argwhere、cov、corrcoef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "56939fda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([1,7,2,19,23,0,88,11,6,11])\n",
    "print(arr1.min()) # 最小值索引 0\n",
    "index = arr1.argmin()\n",
    "arr1[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "b18a9ab9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.argmax() # 计算最大值的索引 返回6\n",
    "# 88就是索引 6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e9240428",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[23],\n",
       "       [88]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = np.argwhere(arr1 > 20)\n",
    "arr1[index]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "8f0286dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   8,  10,  29,  52,  52, 140, 151, 157, 168])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cumsum(arr1) # 计算累加和\n",
    "# 第三个数为什么是10，前三个数相加就是10."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b79bbe63",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9e7f4408",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 5, 7, 8],\n",
       "       [2, 2, 1, 7, 0],\n",
       "       [1, 6, 6, 8, 0],\n",
       "       [3, 8, 9, 6, 8]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "4.6"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.random.randint(0,10,size = (4,5))\n",
    "display(arr2)\n",
    "arr2.mean() # 计算列的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "494d2dee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.75, 5.  , 5.25, 7.  , 4.  ])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2_mean = arr2.mean(axis = 0) #到底计算的是行，还是列\n",
    "# 这是上面列的平均值\n",
    "display(arr2_mean,arr2_mean.shape)\n",
    "\n",
    "# 上面的4和5是二维，为什么下面(5)变成了一维？\n",
    "# arr2.mean(axis = 0)指定了计算维度 axis = 0，\n",
    "# 此时这个0 指向了 二维中的（4，5）中的4.\n",
    "# 把4给计算掉了，所以没有了 。\n",
    "# 最后得出结论就是5."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "06f4e696",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5. , 2.4, 4.2, 6.8])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr2_mean = arr2.mean(axis = 1) \n",
    "display(arr2_mean,arr2_mean.shape)\n",
    "# 具体我们要看 axis 轴的计算方向\n",
    "# axis = 1.就是 计算第一行的 [1, 4, 5, 7, 8]的平均值\n",
    "# 所以打印出来显示 4."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4deb83a2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.040000000000003"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.var(arr2) #计算方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e23ee14c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.040000000000003"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((arr2 - arr2.mean())**2).mean()\n",
    "# 公式如下\n",
    "# 后面的.mean 相当于除以 下面公式的 N"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fafadd5f",
   "metadata": {},
   "source": [
    "$$\n",
    "\\sigma^{2}=\\frac{\\sum\\left(X-\\mu\\right)^{2}}{N} \n",
    "$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "77414eb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 4, 5, 7, 8],\n",
       "       [2, 2, 1, 7, 0],\n",
       "       [1, 6, 6, 8, 0],\n",
       "       [3, 8, 9, 6, 8]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "fb69ab62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.91666667,  1.        ,  1.08333333, -0.66666667,  1.33333333],\n",
       "       [ 1.        ,  6.66666667,  8.33333333, -0.66666667,  5.33333333],\n",
       "       [ 1.08333333,  8.33333333, 10.91666667, -1.        ,  9.33333333],\n",
       "       [-0.66666667, -0.66666667, -1.        ,  0.66666667, -2.66666667],\n",
       "       [ 1.33333333,  5.33333333,  9.33333333, -2.66666667, 21.33333333]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2,rowvar=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a57888ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7.5 ,  1.  ,  1.5 ,  4.  ],\n",
       "       [ 1.  ,  7.3 ,  6.4 , -2.15],\n",
       "       [ 1.5 ,  6.4 , 12.2 ,  2.55],\n",
       "       [ 4.  , -2.15,  2.55,  5.7 ]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2,rowvar=True)\n",
    "# cov就是协方差。矩阵\n",
    "# 协方差就是两列之间的关系。\n",
    "# 两列之间进行计算\n",
    "# 不同学科之间的一个相关性\n",
    "# 比如男生受欢迎程度和什么有相关性呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4d8217c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(6.)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2[0],ddof=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f7d38989",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(6.)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2[0],ddof=0) #协方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b4c179e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6.0"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(((arr2[0] - arr2[0].mean())**2),2).sum()/5 # 方差\n",
    "# 方差有两种计算公式\n",
    "# mean 求和之后求平均\n",
    "# 所以mean就是 sum()/样本数量 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "302ad043",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7.5"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "((arr2[0] - arr2[0].mean())**2).sum()/(5-1)\n",
    "# 数有五个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "966ff326",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(7.5)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.cov(arr2[0])\n",
    "# 7.5与上面的7.5对上了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e83c3134",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.        ,  0.13514748,  0.15681251,  0.61177529],\n",
       "       [ 0.13514748,  1.        ,  0.67817009, -0.33330329],\n",
       "       [ 0.15681251,  0.67817009,  1.        ,  0.30578969],\n",
       "       [ 0.61177529, -0.33330329,  0.30578969,  1.        ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(arr2,rowvar=True) # 相关性系数\n",
    "# 范围-1到1\n",
    "# 要是完全正相关就是 1，完全负相关就是 -1\n",
    "# 比如说，身高越高，体重越大，这就是一个正相关\n",
    "# 如何去衡量? 比如 0.5，0.8，数字越大说明相关性越大"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "eb614d5c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算过程 详解"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20117e52",
   "metadata": {},
   "source": [
    "具体计算过程如下：\n",
    "\n",
    "计算每个变量的均值，即：\n",
    "$\\bar{x}_1 = \\frac{1+2+1+3}{4} = 1.75$\n",
    "$\\bar{x}_2 = \\frac{4+2+6+8}{4} = 5$\n",
    "$\\bar{x}_3 = \\frac{5+1+6+9}{4} = 5.25$\n",
    "$\\bar{x}_4 = \\frac{7+7+8+6}{4} = 7$\n",
    "计算每个变量与其它变量的协方差，即：\n",
    "$cov(x_1,x_2) = \\frac{(1-1.75)(4-5)+(2-1.75)(2-5)+(1-1.75)(6-5)+(3-1.75)(8-5)}{4-1} = 1$\n",
    "$cov(x_1,x_3) = \\frac{(1-1.75)(5-5.25)+(2-1.75)(1-5.25)+(1-1.75)(6-5.25)+(3-1.75)(9-5.25)}{4-1} = 1.5$\n",
    "$cov(x_1,x_4) = \\frac{(1-1.75)(7-7)+(2-1.75)(7-7)+(1-1.75)(8-7)+(3-1.75)(6-7)}{4-1} = 4$\n",
    "$cov(x_2,x_3) = \\frac{(4-5)(5-5.25)+(2-5)(1-5.25)+(6-5)(6-5.25)+(8-5)(9-5.25)}{4-1} = 6.4$\n",
    "$cov(x_2,x_4) = \\frac{(4-5)(7-7)+(2-5)(7-7)+(6-5)(8-7)+(8-5)(6-7)}{4-1} = -2.15$\n",
    "$cov(x_3,x_4) = \\frac{(5-5.25)(7-7)+(1-5.25)(7-7)+(6-5.25)(8-7)+(9-5.25)(6-7)}{4-1} = 2.55$\n",
    "注意，这里使用的是无偏估计的公式，即除以样本数减一。\n",
    "将计算得到的协方差填入协方差矩阵的相应位置，即：\n",
    "$\\begin{bmatrix} 7.5 & 1.0 & 1.5 & 4.0 \\ 1.0 & 7.3 & 6.4 & -2.15 \\ 1.5 & 6.4 & 12.2 & 2.55 \\ 4.0 & -2.15 & 2.55 & 5.7 \\end{bmatrix}$\n",
    "这个矩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8415d7bc",
   "metadata": {},
   "source": [
    "### 线性代数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "404aadba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.array([[4,2,3],\n",
    "              [1,3,1]]) # shape(2,3)\n",
    "B = np.array([[2,7],\n",
    "              [-5,-7],\n",
    "              [9,3]])\n",
    "np.dot(A,B) \n",
    "# 矩阵运算， A的最后一维和B的第一维必须一致\n",
    "# 4，2，3，* 2，-5 ，9. 乘相对应的就得出了四个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7f228e06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 25,  23],\n",
       "       [ -4, -11]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ B # @ 表示矩阵乘积运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "a39e2b47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 7)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = np.random.randint(0,10,size = (3,6))\n",
    "B = np.random.randint(0,10,size = (6,7))\n",
    "A.dot(B).shape\n",
    "# 数是多少不关心 \n",
    "# .shape\n",
    "# 斜对角线 一致。3,6 6,7  6是一样的，\n",
    "# 所以 3和7保留了，斜对角线要相等！！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6a01238c",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)\n",
    "# 如果不想要科学计数法，就写这个就可以，就不是科学计数法了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "181a0b88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  0.5,  0.5],\n",
       "       [ 0. ,  2. , -1. ],\n",
       "       [ 1. , -1.5,  0.5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  0.,  0.],\n",
       "       [ 0.,  1., -0.],\n",
       "       [ 0.,  0.,  1.]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "-1.9999999999999998"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.linalg import inv,det,eig,qr,svd\n",
    "A = np.array([[1,2,3],\n",
    "              [2,3,4], # 如果改成 2，4，6，就报错\n",
    "              [4,5,8]]) # shape(3,3)\n",
    "B = inv(A) # 逆矩阵\n",
    "display(B)\n",
    "display(A @ B) # 逆矩阵\n",
    "det(A) # 计算矩阵行列式 -1.9999999\n",
    "\n",
    "# 这是计算矩阵的 逆\n",
    "# 何为单位矩阵，在第二排数组里面\n",
    "# 1 1 1.斜角线 是 一致的\n",
    "# 逆矩阵必须是 满置矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce0248b9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
